
import * as api from './service'
const Model = {
  namespace: 'manager',
  state: {
    info: {},
    workerInfo: {}, // 员工
    userInfo: {}, // 管理员
    tenantInfo: {}, // 租户平台信息
  },
  effects: {
    *createWorker({payload}, {call}) {
      const res = yield call(api.createWorker, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    *updateWorker({payload}, {call}) {
      const res = yield call(api.updateWorker, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    *getWorkerList({payload}, {call, put}) {
      const res = yield call(api.getWorkerList, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    *getWorkerInfo({payload}, {call, put}) {
      const res = yield call(api.getWorkerInfo, payload)
      if(res) {
        yield put({
          type: 'save',
          payload: {
            key: 'workerInfo',
            data: res.data
          }
        })
      }
    },
    *getUserInfo({ payload }, { call, put}) {
      const res = yield call(api.getUserInfo, payload)
      if(res) {
        yield put({
          type: 'save',
          payload: {
            key: 'userInfo',
            data: res.data
          }
        })

      }
    },
    *getTenantInfo({ payload }, { call, put}) {
      const res = yield call(api.getTenantInfo, payload)
      if(res) {
        yield put({
          type: 'save',
          payload: {
            key: 'tenantInfo',
            data: res.data
          }
        })
      }
    },
    *changePwd({ payload }, { call, put}) {
      const res = yield call(api.changePwd, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 添加项目结算
    *addSettlement({ payload }, { call, put}) {
      const res = yield call(api.addSettlement, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 获取项目结算列表
    *getSettlementList({ payload }, { call, put}) {
      const res = yield call(api.getSettlementList, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 获取日统计
    *getDayStatistics({payload}, { call, put}) {
      const res = yield call(api.getDayStatistics, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 获取月统计
    *getMonthStatistics({payload}, { call, put}) {
      const res = yield call(api.getMonthStatistics, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 记录工时
    *createRecord({payload}, { call, put}) {
      const res = yield call(api.createRecord, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 员工工资支付
    *createPay({payload}, { call, put}) {
      const res = yield call(api.createPay, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
    // 获取员工 工时记录
    *getWorkerRecordList({payload}, { call, put}) {
      const res = yield call(api.getWorkerRecordList, payload)
      return new Promise((resolve, reject) => {
        if(res) {
          resolve(res)
        } else {
          reject()
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return{...state, [action.payload.key]: action.payload.data}
    }
  }
}
export default Model